<?php


namespace app\superpartner\controller;


use app\common\controller\CommonService;
use think\Controller;
use think\Db;
use think\Request;

class OrderBigData extends Controller
{
    /**
     *交易大数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function orderBigInfo(Request $request)
     {
         $superPartnerId = $request->superPartnerId;
         $requestData = $request->post();
         $intervalStart = $requestData['now_start_time'];
         $intervaleEnd = $requestData['now_end_time'];

         $numTime = strtotime($intervaleEnd) - strtotime($intervalStart);
         $num = floor($numTime / 86400);
         //订单数据与交易数据
         for ($i = 0; $i <= $num; $i++) {
             $startTime = date('Y-m-d', strtotime('+' . $i . ' days', strtotime($intervalStart)));
             $endTime = date('Y-m-d', strtotime('+' .  $i + 1 . ' days', strtotime($intervalStart)));
             $time = date('m-d', strtotime($startTime));
             $orderInfo[$i] = $this->getOrderTimeInfo($startTime, $endTime, $superPartnerId, $time);
         }
         return CommonService::success($orderInfo);
     }
    /**
     *获取订单数与交易数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrderTimeInfo($startTime, $endTime, $superPartnerId, $time)
    {
        //订单总数量数量与订单总交易额
        $tbOrder = Db::name('tb_order')
            ->leftJoin('user_manage u','tb_order.uid = u.id')
            ->where('u.super_partner_id',$superPartnerId)
            ->where('order_type', 1)
            ->where('is_weiquan', 0)
            ->whereBetween('order_time', [$startTime, $endTime])
            ->field("count(*) as orderCount,sum(goods_num*goods_price) as orderPrice")
            ->find();
        $jdOrder = Db::name('jd_order')
            ->leftJoin('user_manage u','jd_order.uid = u.id')
            ->where('u.super_partner_id',$superPartnerId)
            ->whereBetween('order_time', [strtotime($startTime )* 1000, strtotime($endTime) * 1000])
            ->field("count(*) as orderCount,sum(goods_num*goods_price) as orderPrice")
            ->find();
        $pddOrder = Db::name('pdd_order')
            ->leftJoin('user_manage u','pdd_order.uid = u.id')
            ->where('u.super_partner_id',$superPartnerId)
            ->whereBetween('order_time', [strtotime($startTime), strtotime($endTime)])
            ->field("count(*) as orderCount,sum(goods_num*goods_price) as orderPrice")
            ->find();
        $data[$time]['orderCount'] = $tbOrder['orderCount'] + $jdOrder['orderCount'] +$pddOrder['orderCount'];
        $data[$time]['orderPrice'] = $tbOrder['orderPrice'] + $jdOrder['orderPrice'] +$pddOrder['orderPrice'];
        //有效订单数量与付款总额
        $tbOrderValid = Db::name('tb_order')
            ->leftJoin('user_manage u','tb_order.uid = u.id')
            ->where('u.super_partner_id',$superPartnerId)
            ->where('order_type', 1)
            ->where('is_weiquan', 0)
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [$startTime, $endTime])
            ->field("count(*) as orderCountValid,sum(user_buy_price) as orderPriceValid")
            ->find();
        $jdOrderValid = Db::name('jd_order')
            ->leftJoin('user_manage u','jd_order.uid = u.id')
            ->where('u.super_partner_id',$superPartnerId)
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [strtotime($startTime )* 1000, strtotime($endTime) * 1000])
            ->field("count(*) as orderCountValid,sum(user_buy_price) as orderPriceValid")
            ->find();
        $pddOrderValid = Db::name('pdd_order')
            ->leftJoin('user_manage u','pdd_order.uid = u.id')
            ->where('u.super_partner_id',$superPartnerId)
            ->where('order_status', '<>', 0)
            ->whereBetween('order_time', [strtotime($startTime), strtotime($endTime)])
            ->field("count(*) as orderCountValid,sum(user_buy_price) as orderPriceValid")
            ->find();
        $data[$time]['orderCountValid'] = $tbOrderValid['orderCountValid'] + $jdOrderValid['orderCountValid'] +$pddOrderValid['orderCountValid'];
        $data[$time]['orderPriceValid'] = $tbOrderValid['orderPriceValid'] + $jdOrderValid['orderPriceValid'] +$pddOrderValid['orderPriceValid'];
        return $data;
    }
}